using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;

using SBPweb.Data;
using SBPweb.Data.SQL2005;

using SBPweb.Common.Exceptions;

namespace SBPweb.PROvibe.Resources.Warehouse.SiteTemplate
{
	public class SiteTemplateRepository : SBPweb.Data.SQL2005.SqlRepository<Resources.Entities.SiteTemplate>
	{

		#region Private fields

		private string mDataBaseName;
		private SqlRepositoryWrapper<Resources.Entities.SiteTemplate> mWrapper;

		#endregion

		#region Constructor logic

		public SiteTemplateRepository(string databaseName)
			: base(databaseName)
		{
			this.mDataBaseName = databaseName;
			this.mWrapper = new SqlRepositoryWrapper<Resources.Entities.SiteTemplate>(this);
		}

		#endregion

        #region Public property accessors

        public SqlRepositoryWrapper<Resources.Entities.SiteTemplate> Wrapper
        {
            get
            {
                return this.mWrapper;
            }
        }

        #endregion

		#region Public methods for repository access

		#region Create

		public void Add(Resources.Entities.SiteTemplate siteTemplate)
		{
			List<SqlParameterDescriptor> parameters = new List<SqlParameterDescriptor>();

			parameters.Add(new SqlParameterDescriptor("SiteId", typeof(Int32), SqlDbType.Int));
			parameters.Add(new SqlParameterDescriptor("TemplateId", typeof(Int32), SqlDbType.Int));

			IInsertFactory<Resources.Entities.SiteTemplate> insertFactory = new InsertCommandFactory<Resources.Entities.SiteTemplate>("PROvibe_CreateSiteTemplate", null, parameters);

			try
			{
				base.Add(insertFactory, siteTemplate);

				Entity.SetActionState(siteTemplate, EntityActionState.Added);
				Entity.SetTransactionState(siteTemplate, EntityTransactionState.Saved);
			}
			catch (SqlException ex)
			{
				ExceptionHandler.HandleSqlException(ex);
			}
		}

		#endregion

		#region Read

		public List<Resources.Entities.SiteTemplate> GetSiteTemplates()
		{
			ISelectionFactory<NullableIdentity> selectionFactory = new ReadAllCommandFactory("PROvibe_ReadSiteTemplate");

			try
			{
				NullableIdentity nullableIdentity = new NullableIdentity();
				return base.Find(selectionFactory, new EntityColumnFactory<Resources.Entities.SiteTemplate>(), nullableIdentity);
			}
			catch (SqlException ex)
			{
				ExceptionHandler.HandleSqlException(ex);
			}

			return new List<Resources.Entities.SiteTemplate>();
		}

		/*
		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesByActorId(Int32 actorId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("ActorId", actorId));

			return GetSiteTemplatesByParameters(pars);
		}

		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesBySiteId(Int32 siteId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("SiteId", siteId));

			return GetSiteTemplatesByParameters(pars);
		}

		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesByRoleId(Int32 roleId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("RoleId", roleId));

			return GetSiteTemplatesByParameters(pars);
		}

		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesByActorIdAndSiteId(Int32 actorId, Int32 siteId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("ActorId", actorId));
			pars.Add(new KeyValuePair<string, object>("SiteId", siteId));

			return GetSiteTemplatesByParameters(pars);
		}

		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesBySiteIdAndRoleId(Int32 siteId, Int32 roleId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("SiteId", siteId));
			pars.Add(new KeyValuePair<string, object>("RoleId", roleId));

			return GetSiteTemplatesByParameters(pars);
		}

		public List<Resources.Entities.SiteTemplate> GetSiteTemplatesByActorIdAndRoleId(Int32 actorId, Int32 roleId)
		{
			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string, object>("ActorId", actorId));
			pars.Add(new KeyValuePair<string, object>("RoleId", roleId));

			return GetSiteTemplatesByParameters(pars);
		}
		*/
		private List<Resources.Entities.SiteTemplate> GetSiteTemplatesByParameters(List<KeyValuePair<string, object>> pars)
		{
			ISelectionFactory<List<KeyValuePair<string, object>>> selectionFactory = new ReadByParameteredCommandFactory("PROvibe_ReadSiteTemplate");

			try
			{
				return base.Find<List<KeyValuePair<string, object>>>(selectionFactory, new EntityColumnFactory<Resources.Entities.SiteTemplate>(), pars);
			}
			catch (SqlException ex)
			{
				ExceptionHandler.HandleSqlException(ex);
			}

			return new List<Resources.Entities.SiteTemplate>();
		}

		#endregion

		#region Update

		public void Update(Resources.Entities.SiteTemplate siteTemplate)
		{
			List<SqlParameterDescriptor> parameters = new List<SqlParameterDescriptor>();

			parameters.Add(new SqlParameterDescriptor("SiteId", typeof(Int32), SqlDbType.Int));
			parameters.Add(new SqlParameterDescriptor("TemplateId", typeof(Int32), SqlDbType.Int));
			
			IUpdateFactory<Resources.Entities.SiteTemplate> updateFactory = new UpdateCommandFactory<Resources.Entities.SiteTemplate>("PROvibe_UpdateSiteTemplate", parameters);

			try
			{
				base.Update(updateFactory, siteTemplate);

				Entity.SetActionState(siteTemplate, EntityActionState.Modified);
				Entity.SetTransactionState(siteTemplate, EntityTransactionState.Saved);
			}
			catch (SqlException ex)
			{
				ExceptionHandler.HandleSqlException(ex);
			}
		}

		#endregion

		#region Delete

		public void Remove(Int32 siteId, Int32 templateId)
		{
			IDeleteFactory<List<KeyValuePair<string, object>>> deleteFactory = new DeleteByParameteredCommandFactory("PROvibe_DeleteSiteTemplate");

			List<KeyValuePair<string, object>> pars = new List<KeyValuePair<string, object>>();
			pars.Add(new KeyValuePair<string,object>("SiteId", siteId));
			pars.Add(new KeyValuePair<string,object>("TemplateId", templateId));

			try
			{
				base.Remove(deleteFactory, pars);
			}
			catch (SqlException ex)
			{
				ExceptionHandler.HandleSqlException(ex);
			}
		}

		#endregion

		#endregion

		#region Public methods for transaction handling

		public override void Commit(Resources.Entities.SiteTemplate siteTemplate)
		{
			if (siteTemplate.TransactionState != EntityTransactionState.Saved)
			{
				ExceptionHandler.HandleException(new Exception("Unsaved entites cannot be commited!"));
			}
			else
			{
				base.Commit(siteTemplate);
				Entity.SetTransactionState(siteTemplate, EntityTransactionState.Committed);
			}
		}

		public override void Rollback(Resources.Entities.SiteTemplate siteTemplate)
		{
			base.Rollback(siteTemplate);

			Entity.SetTransactionState(siteTemplate, EntityTransactionState.Rollbacked);
		}

		#endregion

	}
}
